using System;
using System.IO;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace PupLogReader
{
	/// <summary>
	/// Summary description for frmPupLogViewer.
	/// </summary>
	public class frmPupLogViewer : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TreeView tvPupLogViewer;
		private System.Windows.Forms.Panel pnlPupLogViewer;
		private System.Windows.Forms.Splitter splitter1;
		private System.Windows.Forms.ListView lvPupLogViewerDetail;
		private System.Windows.Forms.OpenFileDialog openFiledlgPupLog;
		private System.Windows.Forms.MainMenu mnuPupLogViewer;
		private System.Windows.Forms.MenuItem mnuItemFile;
		private System.Windows.Forms.MenuItem mnuItemOpen;
		private System.Windows.Forms.MenuItem mnuItemClose;
		private System.Windows.Forms.MenuItem mnuItemRefresh;
		private System.Windows.Forms.MenuItem mnuItemHelp;
		private System.Windows.Forms.MenuItem mnuItemAbout;

		private string fileName = null;
		private const string fileFilter = "pup log files (*.log)|*.log";
		private const string initialDirectory = @"c:\";
		private System.Windows.Forms.ImageList imageLstPupLogViewer;

		private const string beginSitePup = "===================================================";
		private const string endSitePup = "Exit status code for this session:";
		private System.Windows.Forms.ColumnHeader columnHeaderLines;

		private System.ComponentModel.IContainer components;

		public frmPupLogViewer()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmPupLogViewer));
			this.tvPupLogViewer = new System.Windows.Forms.TreeView();
			this.imageLstPupLogViewer = new System.Windows.Forms.ImageList(this.components);
			this.mnuPupLogViewer = new System.Windows.Forms.MainMenu();
			this.mnuItemFile = new System.Windows.Forms.MenuItem();
			this.mnuItemOpen = new System.Windows.Forms.MenuItem();
			this.mnuItemClose = new System.Windows.Forms.MenuItem();
			this.mnuItemRefresh = new System.Windows.Forms.MenuItem();
			this.mnuItemHelp = new System.Windows.Forms.MenuItem();
			this.mnuItemAbout = new System.Windows.Forms.MenuItem();
			this.pnlPupLogViewer = new System.Windows.Forms.Panel();
			this.splitter1 = new System.Windows.Forms.Splitter();
			this.lvPupLogViewerDetail = new System.Windows.Forms.ListView();
			this.columnHeaderLines = new System.Windows.Forms.ColumnHeader();
			this.openFiledlgPupLog = new System.Windows.Forms.OpenFileDialog();
			this.pnlPupLogViewer.SuspendLayout();
			this.SuspendLayout();
			// 
			// tvPupLogViewer
			// 
			this.tvPupLogViewer.Dock = System.Windows.Forms.DockStyle.Fill;
			this.tvPupLogViewer.ImageList = this.imageLstPupLogViewer;
			this.tvPupLogViewer.Location = new System.Drawing.Point(0, 0);
			this.tvPupLogViewer.Name = "tvPupLogViewer";
			this.tvPupLogViewer.Size = new System.Drawing.Size(240, 361);
			this.tvPupLogViewer.TabIndex = 2;
			this.tvPupLogViewer.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.tvPupLogViewer_AfterSelect);
			// 
			// imageLstPupLogViewer
			// 
			this.imageLstPupLogViewer.ImageSize = new System.Drawing.Size(16, 16);
			this.imageLstPupLogViewer.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageLstPupLogViewer.ImageStream")));
			this.imageLstPupLogViewer.TransparentColor = System.Drawing.Color.Transparent;
			// 
			// mnuPupLogViewer
			// 
			this.mnuPupLogViewer.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																							this.mnuItemFile,
																							this.mnuItemHelp});
			// 
			// mnuItemFile
			// 
			this.mnuItemFile.Index = 0;
			this.mnuItemFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																						this.mnuItemOpen,
																						this.mnuItemClose,
																						this.mnuItemRefresh});
			this.mnuItemFile.Text = "File";
			// 
			// mnuItemOpen
			// 
			this.mnuItemOpen.Index = 0;
			this.mnuItemOpen.Text = "Open";
			this.mnuItemOpen.Click += new System.EventHandler(this.mnuItemOpen_Click);
			// 
			// mnuItemClose
			// 
			this.mnuItemClose.Index = 1;
			this.mnuItemClose.Text = "Close";
			this.mnuItemClose.Click += new System.EventHandler(this.mnuItemClose_Click);
			// 
			// mnuItemRefresh
			// 
			this.mnuItemRefresh.Index = 2;
			this.mnuItemRefresh.Text = "Refresh";
			this.mnuItemRefresh.Click += new System.EventHandler(this.mnuItemRefresh_Click);
			// 
			// mnuItemHelp
			// 
			this.mnuItemHelp.Index = 1;
			this.mnuItemHelp.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																						this.mnuItemAbout});
			this.mnuItemHelp.Text = "Help";
			// 
			// mnuItemAbout
			// 
			this.mnuItemAbout.Index = 0;
			this.mnuItemAbout.Text = "About";
			this.mnuItemAbout.Click += new System.EventHandler(this.mnuItemAbout_Click);
			// 
			// pnlPupLogViewer
			// 
			this.pnlPupLogViewer.Controls.Add(this.tvPupLogViewer);
			this.pnlPupLogViewer.Dock = System.Windows.Forms.DockStyle.Left;
			this.pnlPupLogViewer.Location = new System.Drawing.Point(0, 0);
			this.pnlPupLogViewer.Name = "pnlPupLogViewer";
			this.pnlPupLogViewer.Size = new System.Drawing.Size(240, 361);
			this.pnlPupLogViewer.TabIndex = 3;
			// 
			// splitter1
			// 
			this.splitter1.Location = new System.Drawing.Point(240, 0);
			this.splitter1.Name = "splitter1";
			this.splitter1.Size = new System.Drawing.Size(3, 361);
			this.splitter1.TabIndex = 4;
			this.splitter1.TabStop = false;
			// 
			// lvPupLogViewerDetail
			// 
			this.lvPupLogViewerDetail.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																								   this.columnHeaderLines});
			this.lvPupLogViewerDetail.Dock = System.Windows.Forms.DockStyle.Fill;
			this.lvPupLogViewerDetail.Location = new System.Drawing.Point(243, 0);
			this.lvPupLogViewerDetail.Name = "lvPupLogViewerDetail";
			this.lvPupLogViewerDetail.Size = new System.Drawing.Size(309, 361);
			this.lvPupLogViewerDetail.StateImageList = this.imageLstPupLogViewer;
			this.lvPupLogViewerDetail.TabIndex = 5;
			this.lvPupLogViewerDetail.View = System.Windows.Forms.View.Details;
			// 
			// columnHeaderLines
			// 
			this.columnHeaderLines.Text = "Pup Log Details";
			this.columnHeaderLines.Width = 300;
			// 
			// frmPupLogViewer
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(552, 361);
			this.Controls.Add(this.lvPupLogViewerDetail);
			this.Controls.Add(this.splitter1);
			this.Controls.Add(this.pnlPupLogViewer);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Menu = this.mnuPupLogViewer;
			this.Name = "frmPupLogViewer";
			this.Text = "Pup Log Viewer";
			this.pnlPupLogViewer.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new frmPupLogViewer());
		}

		private void mnuItemOpen_Click(object sender, System.EventArgs e)
		{

			openFiledlgPupLog.Filter = fileFilter;
			openFiledlgPupLog.InitialDirectory = initialDirectory;

			if(openFiledlgPupLog.ShowDialog(this)== DialogResult.OK)
			{
				if(openFiledlgPupLog.FileName!= null)
				{
					loadfile(openFiledlgPupLog.FileName);
				}
			}
		}

		private void loadfile(string filename)
		{
			// for now do it the dumb way no algorithm
			fileName = filename;

			// clear the tree and the list
			tvPupLogViewer.Nodes.Clear();
			lvPupLogViewerDetail.Items.Clear();

			// set the root node
			PupNode tn = new PupNode(fileName);
			tn.ImageIndex = 0;
			tn.SelectedImageIndex = 0;

			
			// read the file
			try 
			{
				// Create an instance of StreamReader to read from a file.
				// The using statement also closes the StreamReader.
				using (StreamReader sr = new StreamReader(fileName)) 
				{
					string line;
					// Read and display lines from the file until the end of 
					// the file is reached.
					while ((line = sr.ReadLine()) != null) 
					{
						// find out where we are
						if(line.EndsWith(beginSitePup))
						{
							// if we begining then find out if we are packaging or unpacking
							string line1 = sr.ReadLine();
							PupNode tnpup = new PupNode(line1);
							tn.Nodes.Add(tnpup);
							string line2 = sr.ReadLine();
							string line3 = sr.ReadLine();
							// line three should have the command line used to launch the pup
							if(line3.IndexOf("/u", 0)!=-1)
							{
								tnpup.ImageIndex = 5;
								tnpup.SelectedImageIndex = 5;
							}
							else
							{
								tnpup.ImageIndex = 6;
								tnpup.SelectedImageIndex = 6;
							}

							string pupline;
							while ((pupline = sr.ReadLine()) != null)
							{
								if(pupline.IndexOf(endSitePup, 0)!=0)
								{
									// if we are at the end then find out if we had any errors
									if(pupline.IndexOf("0", 0)!=-1)
									{
										PupNode ts = new PupNode("Successful");
										tnpup.Nodes.Add(ts);
										ts.ImageIndex = 1;
										ts.SelectedImageIndex = 1;

										getPupDetails(line, line1, line2, line3, pupline, ts, sr);
									}
									else
									{
										PupNode tf = new PupNode("Failed");
										tnpup.Nodes.Add(tf);
										tf.ImageIndex = 4;
										tf.SelectedImageIndex = 4;

										getPupDetails(line, line1, line2, line3, pupline, tf, sr);
									}
									break;
								}

								// only write the data if we are not end of pup
								tnpup.Nodes.Add(pupline);
							}
						}
					}
				}
			}
			catch (Exception e) 
			{
				// Let the user know what went wrong.
				MessageBox.Show("The file could not be read:"+"\n"+e.Message);
			}

			tvPupLogViewer.Nodes.Add(tn);
		}

		private void tvPupLogViewer_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
		{
			lvPupLogViewerDetail.Items.Clear();
			AddToDataList(((PupNode)e.Node).lines);
		}

		private void AddToDataList(ArrayList Lines)
		{
			foreach(string s in Lines)
			{
				// do we have any errors?

				ListViewItem lv = new ListViewItem(s);
				if(s.IndexOf(" error", 0)!=-1 || s.IndexOf("FAIL ", 0)!=-1)
				{
					lv.ImageIndex = 3;
					lv.StateImageIndex = 3;
				}
				else
				{
					lv.ImageIndex = 2;
					lv.StateImageIndex = 2;
				}

				lvPupLogViewerDetail.Items.Add(lv);
			}
		}

		private void getPupDetails(string line, string line1, string line2, string line3, string pupline, PupNode tn, StreamReader sr)
		{
			tn.lines.Add(line);
			tn.lines.Add(line1);
			tn.lines.Add(line2);
			tn.lines.Add(line3);
			tn.lines.Add(pupline);

			string puplinedetails;
			while((puplinedetails = sr.ReadLine()) != null)
			{
				tn.lines.Add(puplinedetails);
				if(puplinedetails.EndsWith("Program ending."))
				{
					tn.lines.Add(pupline);
					break;
				}
			}
		}

		private void mnuItemAbout_Click(object sender, System.EventArgs e)
		{
			frmAbout about = new frmAbout();
			about.ShowDialog(this);
		}

		private void mnuItemClose_Click(object sender, System.EventArgs e)
		{
			tvPupLogViewer.Nodes.Clear();
			lvPupLogViewerDetail.Items.Clear();
			fileName = null;
		}

		private void mnuItemRefresh_Click(object sender, System.EventArgs e)
		{
			loadfile(fileName);
		}
	}
}
